import java.lang.reflect.Array;
import java.util.Arrays;

public class Code {

    //填充数组
    public static void main(String[] args) {
        int[] array = new int[10];
        //Arrays.fill(array,9);
        Arrays.fill(array,3,7,9);
        System.out.println(Arrays.toString(array));
    }


    //判断两个数组是否一样
    public static void main18(String[] args) {
        int[] array1 = {1,2,3,4,5,6,7,8,9,10};
        int[] array2 = {1,2,3,4,5,6,7,18,9,10};

        boolean ret = Arrays.equals(array1, array2);
        System.out.println(ret);
    }


    public static void main17(String[] args) {
        int[] array = {1,2,3,4,5,6,7,8,9,10};
        //[3,6)
        int ret = Arrays.binarySearch(array,3,6,7);
        System.out.println(ret);
    }


    //数组逆序
    public static void main16(String[] args) {
        int[] arr = {1, 2, 3, 4};
        reverse(arr);
        System.out.println(Arrays.toString(arr));
    }
    public static void reverse(int[] arr) {
        int left = 0;
        int right = arr.length - 1;
        while (left < right) {
        int tmp = arr[left];
        arr[left] = arr[right];
        arr[right] = tmp;
            left++;
            right--;
            }
    }



    //冒泡排序
    public static void bubbleSort(int[] array){
        for (int i = 0; i < array.length; i++) {
            for (int j = 1; j < array.length-i; j++) {
                if(array[j-1]>array[j]){
                    int tmp = array[j];
                    array[j] = array[j-1];
                    array[j-1] = tmp;
                }
            }
        }
    }

    public static void main15(String[] args) {
        int[] array = {2,6,3,9,1,10};
        bubbleSort(array);
        System.out.println(Arrays.toString(array));
    }


    //对数组进行排序
    public static void main14(String[] args) {
        int[] array = {1,5,2,7,9,8,3,6,4};
        System.out.println("排序前："+Arrays.toString(array));
        Arrays.sort(array);
        System.out.println("排序后："+Arrays.toString(array));

        //二分查找
        int ret = Arrays.binarySearch(array,15);
        System.out.println(ret);
    }

    //二分查找
    public static int binary_Search(int[] array,int val){
        int left = 0;
        int right = array.length-1;
        while(left<=right){
            int mid = (left+right)/2;
            if(array[mid]>val){
                right = mid-1;
            }else if(array[mid]<val){
                left = mid+1;
            }else{
                return mid;
            }
        }
        return -1;
    }

    public static void main13(String[] args) {
        int[] arr= {10,12,15,19,100};
        System.out.println(binary_Search(arr, 19));
    }



    //查找数组中某个数的下标

    /**
     *
     * @param array
     * @param val
     * @return
     */
    public static int fineVal(int[] array,int val){
        for (int i = 0; i < array.length; i++) {
            if(array[i]==val){
                return i;
            }
        }
        return -1;
    }

    public static void main12(String[] args) {
        int[] arr = {1,3,10,78};
        System.out.println(fineVal(arr, 101));
    }



    //模拟实现toString
    public static String myToString(int[] array){
        String ret = "[";
        for (int i = 0; i < array.length; i++) {
            ret += array[i];
            if(i< array.length-1){
                ret += ',';
            }
        }

        ret += "]";
        return ret;
    }

    public static void main11(String[] args) {
        int[] array = {10,20,30};

        String ret = myToString(array);
        System.out.println(ret);
    }


    //将数组作为函数的返回值
    public static int[] test(){
        int[] ret = new int[2];
        ret[0] = 1;
        ret[1] = 2;
        return ret;
    }

    public static void main10(String[] args) {
        int[] array = test();
        System.out.println(Arrays.toString(array));
    }


    public static void fun1(int[] array){
        array = new int[]{1,2,3,4};
    }

    public static void fun2(int[] array){
        array[0] = 100;
    }

    public static void print(int[] array){
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]+" ");
        }
    }
    public static void main9(String[] args) {
        int[] array = {10,20,30,40};
        fun2(array);
        print(array);
    }


    public static void main8(String[] args) {
        int[] array1 = null;
        System.out.println(array1.length);
    }

    public static void main7(String[] args) {
        int[] array3 = {1,2,3,4};
        int[] array4 = {100,300};
        System.out.println("交换前的array3："+Arrays.toString(array3));
        System.out.println("交换前的array4："+Arrays.toString(array4));

        int[] tmp = array3;
        array3 = array4;
        array4 = tmp;
        System.out.println("交换后的array3："+Arrays.toString(array3));
        System.out.println("交换后的array4："+Arrays.toString(array4));
    }


    public static void main6(String[] args) {

        int[] array1 = new int[3];
        array1[0] = 10;
        array1[1] = 20;
        array1[2] = 30;


        int[] array2 = new int[]{1,2,3,4,5};
        array2[0] = 100;
        array2[1] = 200;

        array1 = array2;
        array1[2] = 300;
        array1[3] = 400;
        array2[4] = 500;

        for (int i = 0; i < array2.length; i++) {
            System.out.println(array2[i]);
        }

    }



    public static void main5(String[] args) {
        int[] array = {1,2,3,4,5};
        //引用类型和基本类型的区别
    }


    //遍历数组
    public static void main4(String[] args) {
        int[] array = new int[]{1,2,3,4,5};
        int len = array.length;
        for (int i = 0; i < len; i++) {
            //System.out.println(array[i]);
            System.out.print(array[i]+" ");
        }
        System.out.println();
        //第二种打印方式
        //for each 循环
        for(int x:array){
            System.out.print(x+" ");
        }
        System.out.println();
        //第三种打印方法
        //把传入的数组 转化为 字符串的形式进行输出
        String ret = Arrays.toString(array);
        System.out.println(ret);
    }


    public static void main3(String[] args) {
        int[] array = {1,2,3,4};
        //System.out.println(array[100]);

        System.out.println(array[1]);
        array[1] = 99;
        System.out.println(array[1]);

    }


    public static void main2(String[] args) {
        int[] array = {1,2,3,4};

        int[] array1 = new int[3];

        int[] array2 = new int[]{1,2,3,4,5,6};

        boolean[] array3 = new boolean[3];
        //默认值是false

        String[] strings = new String[3];
        System.out.println("=====");
    }




//汉诺塔
  public static void hanoi(int n,char S,char T,char D){
      if(n==1){
          System.out.println("from"+S+"to"+D);
      }
      else{
          hanoi(n-1,S,D,T);
          System.out.println("from"+S+"to"+D);
          hanoi(n-1,T,S,D);
      }
  }

    public static void main1(String[] args) {
        hanoi(3,'A','B','C');
    }

    }



